package com.ivanceras.server.db;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Properties;
import java.util.Queue;

import com.ivanceras.server.util.Configuration;
import com.ivanceras.shared.db.IDatabase;
import com.ivanceras.shared.exception.DatabaseException;




public class DBPool
{
	final static DBPool singleton = new DBPool();
	final static String etcCustomConfigurationFile = Configuration.etcCustomConfigurationFile;
	static String databasePlatform = null;
	public static final DBPool getDBPool()  { return singleton; }
	
	//////////////////////////////////
	// SINGLETON STATE VARIABLES
	//
	
	Queue<IDatabase> pool = new LinkedList<IDatabase>();
	
	public IDatabase connectDB() throws DatabaseException
	{
		return connectDB(null);
	}
	
	public synchronized IDatabase connectDB(String propertyFile) throws DatabaseException
	{
		System.err.println("DB DEBUG:   Number of cached connections: "+pool.size());
		IDatabase ret = pool.poll();
		if(ret!=null) while( ret.isClosed() ) ret=pool.poll();
		
		if(ret==null || ret.isClosed()) ret = newConnection(propertyFile);
		return ret;
	}
	
	public synchronized void releaseConnection(IDatabase con)
	{
		if(con.isClosed()) return;
		
		con.reset();
		pool.add(con);
	}
	
	public static IDatabase newConnection(String propertyFile) throws DatabaseException
	{
		Properties prop = new Properties();
		prop = getDefaultProperties();
		Properties customProp = null;
		if(DatabaseManager.BIGTABLE.equals(prop.getProperty("databaseType")) == false){//bigtable can't read file
			System.out.println("databaseType: ["+prop.getProperty("databaseType")+"] not equal to ["+DatabaseManager.BIGTABLE+"]");
			customProp = getEtcCustomConfigurationFile();
		}
		if(customProp != null){
			return DatabaseManager.create(prop, customProp);
		}else{
			return DatabaseManager.create(prop);
		}
	}
	
	public static Properties getDefaultProperties(){
		Properties prop = new Properties();
		String dbType = Configuration.dbType;
		databasePlatform = dbType;
		String dbHost = Configuration.dbHost;
		String dbPort = Configuration.dbPort;
		String dbName = Configuration.dbName;
		String dbUser = Configuration.dbUser;
		String dbPassword = Configuration.dbPassword;
		String dbSchema = Configuration.dbSchema;
		
		prop.setProperty("databaseType",dbType);
		prop.setProperty("databaseHost", dbHost);
		prop.setProperty("databasePort",dbPort);
		prop.setProperty("databaseName", dbName);
		prop.setProperty("databaseUser", dbUser);
		prop.setProperty("databasePassword", dbPassword);
		prop.setProperty("defaultSchema", dbSchema);
		return prop;
	}
	
	
	/**
	 * get the Properties of /etc/ws/ws.properties
	 * @return
	 */
	public static Properties getEtcCustomConfigurationFile(){
		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream(etcCustomConfigurationFile));
			System.out.println("Found custom config file: ["+etcCustomConfigurationFile+"]");
			return prop;
		} catch (FileNotFoundException e) {
			System.out.println("No custom config file: "+etcCustomConfigurationFile);
			System.out.println("Using hard coded config.");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e){
			e.printStackTrace();
			return null;
		}
		return null;
	}
}
